<?php


namespace Modules\System\Http\Controllers\Admin;


use App\Http\Controllers\MyController;
use Illuminate\Http\Request;
use Modules\System\Http\Requests\AttrRequest;
use Modules\System\Models\Attr;

class AttrController extends MyController
{

    public function index(Request $request)
    {
        if ($request->ajax() && $request->wantsJson()) {
            $data = Attr::orderBy('id', 'desc')
                ->paginate($this->request('limit', 'intval'))->toArray();

            return $this->jsonSuc($data);
        }

        return $this->view('admin.attr.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return $this->view('admin.attr.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(AttrRequest $request, Attr $attr)
    {
        $data = $request->validated();
        $result = $attr->store($data);

        return $this->result($result);
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $attr = Attr::find($this->request('id', 'intval'));

        return $this->view('admin.attr.edit', compact('attr'));
    }

    /**
     * 更新
     */
    public function update(AttrRequest $request, Attr $attr)
    {

        if ($id = $this->request('id', 'intval')) {

            $data = $request->validated();
            $data['id'] = $id;

            $result = $attr->up($data);

            return $this->result($result);
        }

        return $this->result(false);
    }


    /**
     * 删除
     */
    public function destroy()
    {
        $result = Attr::destroy($this->request('id', 'intval'));
        return $this->result($result);
    }

}
